package com.lovo.dao.impl;

import com.lovo.dao.BaseDAO;
import com.lovo.dao.UserDAO;
import com.lovo.pojo.UserDO;

import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {

    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        con = getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_user_name=? and u_password=?";
        //2. 获取执行SQL语句执行,并返回结果
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            //3. 执行SQL语句
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_user_name"));
                user.setPwd(rs.getString("u_password"));
                user.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            closeConnection(con, statement, rs);
        }
        return user;
    }

    @Override
    public UserDO getByUsername(String username) {
        con = getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_user_name=?";
        //2. 获取执行SQL语句执行,并返回结果
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            //3. 执行SQL语句
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_user_name"));
                user.setPwd(rs.getString("u_password"));
                user.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            closeConnection(con, statement, rs);
        }
        return user;
    }

    @Override
    public UserDO getById(int id) {
        con = getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_id=?";
        //2. 获取执行SQL语句执行,并返回结果
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            //3. 执行SQL语句
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setAccount(rs.getString("u_user_name"));
                user.setPwd(rs.getString("u_password"));
                user.setBalance(rs.getDouble("u_balance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            closeConnection(con, statement, rs);
        }
        return user;
    }

    @Override
    public void update(UserDO user) {
        //1. 建立连接
        con = getConnection();
        statement = null;
        String sql = "update t_user set u_password=?,u_balance=? where u_id=?";
        //2. 获取执行SQL语句执行对象
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getPwd());
            statement.setObject(2, user.getBalance());
            statement.setObject(3, user.getId());
            //3. 执行SQL语句
            int row = statement.executeUpdate();
//            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            closeConnection(con, statement);
        }
    }

    @Override
    public void insert(UserDO user) {
        //1. 建立连接
        con = getConnection();
        statement = null;
        String sql = "insert into t_user(u_user_name,u_password,u_balance) values (?,?,?)";
        //2. 获取执行SQL语句执行对象
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getAccount());
            statement.setObject(2, user.getPwd());
            statement.setObject(3, user.getBalance());
            //3. 执行SQL语句
            int row = statement.executeUpdate();
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            closeConnection(con, statement);
        }
    }
}
